var app = getApp()
var QRCode = require('../../../../../utils/weapp-qrcode.js');
var verify = require('../../../../../utils/verify-token.js')
var qrcode;
var qrcode1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    now:"deviceInfo"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("basicInfo",options)

    var deviceid = options.deviceid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this
  
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      deviceid: deviceid,
    })
    // 获取设备信息
    wx.request({
      url: baseUrl+'device/'+deviceid+'/device_info',
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var deviceInfo = res.data.data
          if (deviceInfo.productIdentifier != null) {
            var productIdentifier = deviceInfo.productIdentifier
            var length = 5 - productIdentifier.length
            for (var j = 0; j < length; j++) {
              productIdentifier = "0" + productIdentifier
            }
            productIdentifier = "PDM" + productIdentifier
            deviceInfo.productIdentifier = productIdentifier
            console.log("deviceInfo", deviceInfo)
          }
          that.setData({
            deviceInfo: deviceInfo,
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        
      }
    })

    // 获取使用信息
    wx.request({
      url: baseUrl + 'device/' + deviceid + '/use_info',
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("useList", res.data.data)
          var useInfo = res.data.data
          if (useInfo.locationIdentifier != null) {
            var locationIdentifier = useInfo.locationIdentifier
            var length = 5 - locationIdentifier.length
            for (var j = 0; j < length; j++) {
              locationIdentifier = "0" + locationIdentifier
            }
            locationIdentifier = "LOC" + locationIdentifier
            useInfo.locationIdentifier = locationIdentifier
          }
          that.setData({
            useInfo: useInfo,
          })
          if (useInfo.orderQrCode != null) {
            qrcode1 = new QRCode('canvas1', {
              text: useInfo.orderQrCode,
              width: 130,
              height: 130,
              colorDark: "#000000",
              colorLight: "#ffffff",
              correctLevel: QRCode.CorrectLevel.H,
            });
          }
          if (useInfo.qrCode != null) {
            qrcode = new QRCode('canvas', {
              text: useInfo.qrCode,
              width: 130,
              height: 130,
              colorDark: "#000000",
              colorLight: "#ffffff",
              correctLevel: QRCode.CorrectLevel.H,
            });
          }
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })

    
  },

  // 切换设备信息类型
  change:function(res){
    var now = res.currentTarget.dataset.now
    if (now =="useInfo"){
      console.log("useInfo")
      var code = this.data.useInfo.qrCode
      var orderCode = this.data.useInfo.orderQrCode
      setTimeout(function () {
        if(code!=null){
          qrcode.makeCode(code);
        }
        if(orderCode!=null){
          qrcode1.makeCode(orderCode);
        }
      }, 1000)
    }
    this.setData({
      now:now
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})